﻿using CPPEI.Coolzon.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for BrushEditWindow.xaml
    /// </summary>
    public partial class BrushEditWindow : Window
    {
        /// <summary>
        /// 返回画刷
        /// </summary>
        public BrushEx ReturnBrush { get; private set; }
        private string _rdName = "Solid";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="brush"></param>
        public BrushEditWindow(BrushEx brush)
        {
            InitializeComponent();

            ReturnBrush = brush;

            _ctlBrushSolid.IsChecked = true;
            if(ReturnBrush == null)
            {
                _ctlBrushNull.IsChecked = true;
            }
            else if (ReturnBrush.Brush is SolidColorBrush)
            {
                _ctlBrushSolid.IsChecked = true;
            }
            else if (ReturnBrush.Brush is ImageBrush)
            {
                _ctlBrushImage.IsChecked = true;
                ImageBrush imageBrush = ReturnBrush.Brush as ImageBrush;
                BitmapImage bitmapImage = (BitmapImage)imageBrush.ImageSource;

                imagePreview.Background = ReturnBrush.Brush;
                cbStretch.SelectedItem = imageBrush.Stretch;
                cbTileMode.SelectedItem = imageBrush.TileMode;
            }
            else if (ReturnBrush.Brush is LinearGradientBrush)
            {
                _ctlBrushGradient.IsChecked = true;
            }

            this.KeyUp += BrushEditWindow_KeyUp;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Uri fullPath = null;
            if (ReturnBrush != null && ReturnBrush.Brush is ImageBrush)
            {
                BitmapImage bitmapImage = (BitmapImage)(ReturnBrush.Brush as ImageBrush).ImageSource;
                fullPath = new Uri(ReturnBrush.Path);
            }
            Coolzon.Designtime.ImageRes selected = null;
            Coolzon.Designtime.ImageRes[] imageList = Coolzon.Designtime.ResPoolFactory.GetImageResPool().GetImages();
            if (imageList != null && imageList.Length > 0)
            {
                foreach (var item in imageList)
                {
                    cbPicName.Items.Add(item);
                    if (fullPath != null && fullPath == new Uri(item.FullPath))
                    {
                        selected = item;
                    }
                }
                if (selected != null)
                {
                    cbPicName.SelectedItem = selected;
                }
            }
        }

        void BrushEditWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                colorSlider.DelectRect();
            }
        }
        private void _ctlBtnOK_Click_1(object sender, RoutedEventArgs e)
        {
            if (_rdName.Equals("Null"))
            {
                ReturnBrush = null;
            }
            else if (_rdName.Equals("Solid"))
            {
                ReturnBrush = new BrushEx(new SolidColorBrush(colorPlate.SelectedColor.Value));
            }
            else if (_rdName.Equals("Image"))
            {
                ImageBrush imageBrush = ReturnBrush.Brush as ImageBrush;
                if (cbStretch.SelectedItem != null)
                {
                    imageBrush.Stretch = (Stretch)cbStretch.SelectedItem;
                }
                if (cbTileMode.SelectedItem != null)
                {
                    imageBrush.TileMode = (TileMode)cbTileMode.SelectedItem;
                }
            }
            else if (_rdName.Equals("Gradient"))
            {
                ReturnBrush = new BrushEx(colorSlider.SelectedBrush);
            }
            DialogResult = true;
        }

        private void cbPicName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbPicName.SelectedItem != null)
            {
                try
                {
                    Coolzon.Designtime.ImageRes imageRes = cbPicName.SelectedItem as Coolzon.Designtime.ImageRes;

                    //BitmapImage image = new BitmapImage(new Uri(imageRes.FullPath));

                    BitmapImage image = new BitmapImage();
                    FileInfo file = new FileInfo(imageRes.FullPath);
                    if (file != null && file.Exists)
                    {
                        using (FileStream fileStream = File.Open(file.FullName, FileMode.Open, FileAccess.Read))
                        {
                            using (BinaryReader binReader = new BinaryReader(fileStream))
                            {
                                byte[] bytes = binReader.ReadBytes((int)file.Length);

                                image = new BitmapImage();
                                image.CacheOption = BitmapCacheOption.OnLoad;
                                image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                                image.BeginInit();
                                image.StreamSource = new MemoryStream(bytes);
                                image.EndInit();
                            }
                        }
                    }

                    ReturnBrush = new BrushEx(new ImageBrush(image), imageRes.FullPath);

                    imagePreview.Background = ReturnBrush.Brush;
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTAG, "BrushEditWindow 加载选择图片出错,{0}", ex);
                }
            }
        }

        private void cbTileMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ImageBrush imageBrush = ReturnBrush.Brush as ImageBrush;
            if (imageBrush != null)
            {
                imageBrush.TileMode = (TileMode)cbTileMode.SelectedValue;
            }
        }

        private void cbStretch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ImageBrush imageBrush = ReturnBrush.Brush as ImageBrush;
            if (imageBrush != null)
            {
                imageBrush.Stretch = (Stretch)cbStretch.SelectedValue;
            }
        }

        private void _ctlBrushNull_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            _rdName = rb.Content.ToString();
            colorPlate.Visibility = Visibility.Collapsed;
            gdImage.Visibility = Visibility.Collapsed;
            colorSlider.Visibility = Visibility.Collapsed;
            this.Width = 257;
            this.Height = 328;
        }

        private void _ctlBrushSolid_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            _rdName = rb.Content.ToString();
            colorPlate.Visibility = Visibility.Visible;
            gdImage.Visibility = Visibility.Collapsed;
            colorSlider.Visibility = Visibility.Collapsed;

            //初始化为不透明色
            colorPlate.A = 0xFF;
            if (ReturnBrush != null && ReturnBrush.Brush is SolidColorBrush)
            {
                colorPlate.SelectedColor = (ReturnBrush.Brush as SolidColorBrush).Color;
            }
            else
            {
                colorPlate.SelectedColor = Colors.White;
            }
            this.Width = 257;
            this.Height = 328;
        }

        private void _ctlBrushGradient_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            _rdName = rb.Content.ToString();
            colorPlate.Visibility = Visibility.Collapsed;
            gdImage.Visibility = Visibility.Collapsed;
            colorSlider.Visibility = Visibility.Visible;
            if (ReturnBrush != null)
                colorSlider.InitColorGradient(ReturnBrush.Brush);
            this.Width = 563;
            this.Height = 387;
        }

        private void _ctlBrushImage_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            _rdName = rb.Content.ToString();
            colorPlate.Visibility = Visibility.Collapsed;
            gdImage.Visibility = Visibility.Visible;
            colorSlider.Visibility = Visibility.Collapsed;
            this.Width = 257;
            this.Height = 328;
        }
    }
}
